package com.ruoyi.jjyh.controller;

import java.util.List;

import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.PingYinUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.jjyh.domain.JjyhGoodsclass;
import com.ruoyi.jjyh.service.IJjyhGoodsclassService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.Ztree;

/**
 * 商品分类Controller
 * 
 * @author ruoyi
 * @date 2020-11-19
 */
@Controller
@RequestMapping("/jjyh/goodsclass")
public class JjyhGoodsclassController extends BaseController
{
    private String prefix = "jjyh/goodsclass";

    @Autowired
    private IJjyhGoodsclassService jjyhGoodsclassService;

    @RequiresPermissions("jjyh:goodsclass:view")
    @GetMapping()
    public String goodsclass()
    {
        return prefix + "/goodsclass";
    }

    /**
     * 查询商品分类树列表
     */
    @RequiresPermissions("jjyh:goodsclass:list")
    @PostMapping("/list")
    @ResponseBody
    public List<JjyhGoodsclass> list(JjyhGoodsclass jjyhGoodsclass)
    {
        List<JjyhGoodsclass> list = jjyhGoodsclassService.selectJjyhGoodsclassList(jjyhGoodsclass);
        return list;
    }

    /**
     * 导出商品分类列表
     */
    @RequiresPermissions("jjyh:goodsclass:export")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JjyhGoodsclass jjyhGoodsclass)
    {
        List<JjyhGoodsclass> list = jjyhGoodsclassService.selectJjyhGoodsclassList(jjyhGoodsclass);
        ExcelUtil<JjyhGoodsclass> util = new ExcelUtil<JjyhGoodsclass>(JjyhGoodsclass.class);
        return util.exportExcel(list, "goodsclass");
    }

    /**
     * 新增商品分类
     */
    @GetMapping(value = { "/add/{id}", "/add/" })
    public String add(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("jjyhGoodsclass", jjyhGoodsclassService.selectJjyhGoodsclassById(id.toString()));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存商品分类
     */
    @RequiresPermissions("jjyh:goodsclass:add")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JjyhGoodsclass jjyhGoodsclass)
    {
        jjyhGoodsclass.setClassCode(PingYinUtil.getFullSpell(jjyhGoodsclass.getClassName()));
        if(EmptyUtils.isEmpty(jjyhGoodsclass.getPid())){
            jjyhGoodsclass.setPid("0");
            jjyhGoodsclass.setClassLevel("一级");
        }else {
            JjyhGoodsclass temp = jjyhGoodsclassService.selectJjyhGoodsclassById(jjyhGoodsclass.getPid());
            if("0".equals(temp.getPid())){
                jjyhGoodsclass.setClassLevel("二级");
            }else{
                jjyhGoodsclass.setClassLevel("三级");
            }
        }
        return toAjax(jjyhGoodsclassService.insertJjyhGoodsclass(jjyhGoodsclass));
    }

    /**
     * 修改商品分类
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        JjyhGoodsclass jjyhGoodsclass = jjyhGoodsclassService.selectJjyhGoodsclassById(id);
        mmap.put("jjyhGoodsclass", jjyhGoodsclass);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品分类
     */
    @RequiresPermissions("jjyh:goodsclass:edit")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JjyhGoodsclass jjyhGoodsclass)
    {
        jjyhGoodsclass.setClassCode(PingYinUtil.getFullSpell(jjyhGoodsclass.getClassName()));
        if(EmptyUtils.isEmpty(jjyhGoodsclass.getPid())){
            jjyhGoodsclass.setPid("0");
            jjyhGoodsclass.setClassLevel("一级");
        }else {
            JjyhGoodsclass temp = jjyhGoodsclassService.selectJjyhGoodsclassById(jjyhGoodsclass.getPid());
            if("0".equals(temp.getPid())){
                jjyhGoodsclass.setClassLevel("二级");
            }else{
                jjyhGoodsclass.setClassLevel("三级");
            }
        }
        return toAjax(jjyhGoodsclassService.updateJjyhGoodsclass(jjyhGoodsclass));
    }

    /**
     * 删除
     */
    @RequiresPermissions("jjyh:goodsclass:remove")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") String id)
    {
        return toAjax(jjyhGoodsclassService.deleteJjyhGoodsclassById(id));
    }

    /**
     * 选择商品分类树
     */
    @GetMapping(value = { "/selectGoodsclassTree/{id}", "/selectGoodsclassTree/" })
    public String selectGoodsclassTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("jjyhGoodsclass", jjyhGoodsclassService.selectJjyhGoodsclassById(id.toString()));
        }
        return prefix + "/tree";
    }

    /**
     * 加载商品分类树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = jjyhGoodsclassService.selectJjyhGoodsclassTree();
        return ztrees;
    }
}
